var _ajax = require('../../utils/ajax'); // 请求函数
var _Server = require('../../utils/server');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        number: 1,
        type: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var orderId = options.orderId;
        // 请求套餐详情数据
        _ajax.myProductDetails(options.productId, function (data) {
            if(data.code == 0){
                that.setData({
                    indexUrl: _Server.url,
                    orderId: options.orderId,
                    myPayData: data.data,
                    yMoney: data.data.price, // 全款
                    dMoney: data.data.deposit, // 预付款
                    money: data.data.price, // 切换时的金额
                });
            }
        }, this);
    },
    radioChange(e) {
        var that = this;
        var value = e.detail.value;
        if(value == 1){
            that.setData({
                money: that.data.yMoney
            });
        } else if(value == 2){
            that.setData({
                money: that.data.dMoney
            });
        };
        that.setData({
            type: value
        });
    },
    // 去支付
    fnWxPay: function () {
        var that = this;
        var orderId = that.data.orderId;
        _ajax.myProductPay(orderId, that.data.type, function (data) {
            var data = data.data;
            wx.requestPayment({
                timeStamp: data.timeStamp,
                nonceStr: data.nonceStr,
                package: data.package,
                signType: data.signType,
                paySign: data.paySign,
                success(res) { // 支付成功回调
                    wx.redirectTo({
                        url: '../myProduct/myProduct',
                    });
                },
                fail(res) {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'loading',
                        duration: 800
                    });
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        });
                    }, 800);
                }
            })
        }, this);
    }
})